/**
**********************************************************************
* @file     main.c
* @brief    对文件的简要说明
* @details  对文件的详细说明
* @author   jcai
* @date     2022-05-06 17:02:14
* @copyright Copyright &copy; 2022 wy1107412334@163.com All rights reserved
* @version  v0.0.1
*
* \n Email:  wy1107412334@163.com
* \n Blog:   None
* \n Github: https://github.com/wy1107412334
* \n Gitee:  https://gitee.com/git.git1107412334.com
*
**********************************************************************
* @note
* None.
**********************************************************************
* @attention
* None.
**********************************************************************
@verbatim
@endverbatim
**********************************************************************
* @par 历史记录
* 1. v0.0.1: jcai, 2022-05-06 17:02:14
*   - 首次编写
*
*<center>Copyright &copy; 2022 wy1107412334@163.com All rights reserved</center>
**********************************************************************
*/

/* 头文件导入区 --------------------------------------------------- */

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <SDL/SDL_keyboard.h>
#include <SDL/SDL_mouse.h>
#include <SDL/SDL_stdinc.h>
#include <SDL/SDL_events.h>
#include <SDL/SDL_timer.h>
#include <SDL/SDL_keysym.h>



typedef struct
{
    int startTicks;
    int pausedTicks;
    int paused;
    int started;
} Timer_t;


// 窗口属性
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

// 帧速率
const int FRAMES_PER_SECOND = 20;

// 事件结构体
SDL_Event event;

// 渲染标志
int renderQuad = 1;



int initGL()
{
    //Initialize Projection Matrix
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

    //Initialize Modelview Matrix
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    //Initialize clear color
    glClearColor( 0.f, 0.f, 0.f, 1.f );

    //Check for error
    GLenum error = glGetError();
    if( error != GL_NO_ERROR )
    {
        printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) );
        return 0;
    }

    return 1;
}

int init()
{
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
        return -1;

    // 创建窗口
    if( SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,
                         SDL_OPENGL) == NULL )
    {
        SDL_Quit();
        return -2;
    }

    // 启用 UNICODE
    SDL_EnableUNICODE(SDL_TRUE);

    // 初始化 OpenGL
    if (!initGL())
    {
        SDL_Quit();
        return -3;
    }

    SDL_WM_SetCaption("OpenGL Test. Press q key", NULL );

    return 0;
}

void clean_up()
{
    SDL_Quit();
}

void handleKeys( unsigned char key, int x, int y )
{
    //Toggle quad
    if( key == 'q' )
    {
        renderQuad = !renderQuad;
    }
}

void update()
{

}

void render()
{
    //Clear color buffer
    glClear( GL_COLOR_BUFFER_BIT );

    //Render quad
    if(renderQuad)
    {
        glBegin( GL_QUADS );
            glVertex2f( -0.5f, -0.5f );
            glVertex2f(  0.5f, -0.5f );
            glVertex2f(  0.5f,  0.5f );
            glVertex2f( -0.5f,  0.5f );
        glEnd();
    }

    //Update screen
    SDL_GL_SwapBuffers();
}


void timer_init(Timer_t *tm)
{
    tm->startTicks = 0;
    tm->pausedTicks = 0;
    tm->paused = 0;
    tm->started = 0;
}
void timer_start(Timer_t *tm)
{
    tm->started = 1;
    tm->paused = 0;

    tm->startTicks = SDL_GetTicks();
}
void timer_stop(Timer_t *tm)
{
    tm->started = 0;
}
void timer_pause(Timer_t *tm)
{
    if (tm->started && !tm->paused)
    {
        tm->paused = 1;
        tm->pausedTicks = SDL_GetTicks() - tm->startTicks;
    }
}
void timer_unpause(Timer_t *tm)
{
    if (tm->paused)
    {
        tm->paused = 0;

        tm->startTicks = SDL_GetTicks() - tm->pausedTicks;
        tm->pausedTicks = 0;
    }
}
int timer_get_ticks(Timer_t *tm)
{
    if (tm->started)
    {
        if (tm->paused)
            return tm->pausedTicks;
        else
            return SDL_GetTicks() - tm->startTicks;
    }
    return 0;
}
int timer_is_started(Timer_t *tm)
{
    return tm->started;
}
int timer_is_paused(Timer_t *tm)
{
    return tm->paused;
}



/**
**********************************************************************
* @brief    简要说明
* @details  详细说明
* @param    none
* @param[in]     a: 该参数用于输入
* @param[in,out] b: 该参数用于输入和输出
* @param[out]    c: 该参数用于输出
* @retval  none
* @retval  0 函数成功
* @retval  1 函数失败
* @return  返回0成功；否则失败
* @note    none
*/
int main(int argc, char *argv[])
{
    Timer_t fps;
    // 退出标志
    int quit = 0;

    if (init() != 0)
    {
        fprintf(stderr, "SDL初始化失败\n");
        return EXIT_FAILURE;
    }

    timer_init(&fps);

    while (!quit)
    {
        timer_start(&fps);

        // 处理SDL的事件
        while (SDL_PollEvent(&event))
        {
            // 如果用户点击了窗口右上角的关闭按钮
            if (event.type == SDL_QUIT)
            {
                quit = 1;
                break;
            }
            else if (event.type == SDL_KEYDOWN)
            {
                int x = 0, y = 0;
                SDL_GetMouseState(&x, &y);
                handleKeys(event.key.keysym.unicode, x, y);
            }
        }

        // 更新帧
        update();

        // 渲染帧
        render();

        // 控制帧速率
        if (timer_get_ticks(&fps) < (1000 / FRAMES_PER_SECOND))
            SDL_Delay((1000 / FRAMES_PER_SECOND) - timer_get_ticks(&fps));
    }

    clean_up();

    return 0;
}


/***  Copyright (C) 2022 wy1107412334@163.com All rights reserved ***/
/****************************** END OF FILE *************************/

